package com.marvels.system.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.system.common.dao.McsmRevealConfigureAppDao;
import com.marvels.system.common.dto.McsmRevealConfigureAppDTO;
import com.marvels.system.common.service.SysConfigService;


/**
 * 系统配置impl
 * @author 唐佩文
 *
 */
@Service
public class SysConfigImpl implements SysConfigService{

	@Value("${config.properties.path}")
	private String configPropertiesPath;
	
	@Autowired
	private McsmRevealConfigureAppDao mcsmRevealConfigureAppDao;
	
	@Override
	public Map<String,Object> appRevealConfigure(McsmRevealConfigureAppDTO appDTO)
			throws MarvelsBusinessException {
		appDTO = new  McsmRevealConfigureAppDTO();
		appDTO.setConfigType(1);
		appDTO.setState(0);
		List<McsmRevealConfigureAppDTO> list= mcsmRevealConfigureAppDao.selectByConfigKey(appDTO);
		MarvelsLogUtil.getInstance().info("参数："+ JSONObject.toJSONString(appDTO) +",服务热线查询结果：" + JSONObject.toJSONString(list));
		Map<String,Object> map=new HashMap<String,Object>();
		if(CollectionUtils.isNotEmpty(list)){
			for(McsmRevealConfigureAppDTO dto:list){
				if(dto.getState() == 0){
					map.put(dto.getConfigKey()+"_value", dto.getConfigValue());
					map.put(dto.getConfigKey()+"_desc", dto.getConfigDesc());
				}
			}
		}
		map.put("list", list);
		return map;
	}

	@Override
	public List<McsmRevealConfigureAppDTO> queryConfigList(McsmRevealConfigureAppDTO configDto)
			throws MarvelsBusinessException {
		return mcsmRevealConfigureAppDao.queryConfigList(configDto);
	}

	@Override
	public McsmRevealConfigureAppDTO updateConfigByKey(McsmRevealConfigureAppDTO configDto) throws MarvelsBusinessException {
		mcsmRevealConfigureAppDao.updateConfigByKey(configDto);
		return mcsmRevealConfigureAppDao.queryConfigByKey(configDto.getConfigKey());
	}

	@Override
	public List<McsmRevealConfigureAppDTO> queryPayChannelList() throws MarvelsBusinessException {
		String defaultChannel  = ServerProperties.getValue(configPropertiesPath, "common.properties", "payment_channel");
		return mcsmRevealConfigureAppDao.queryPayChannelList(defaultChannel);
	}

}
